package org.lex.splitter.util;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;

import org.apache.log4j.Logger;

public class FileChooserUtils {
	private static final Logger log = Logger.getLogger(FileChooserUtils.class);

	public static File chooseFile(Frame parent, File folder, boolean file) {
		log.info("Choose File baseDir=" + folder + ", type=" + (file ? "FILE" : "FOLDER"));
		FileDialog chooser = new FileDialog(parent);
		chooser.setLocationRelativeTo(parent);
		chooser.setMode(FileDialog.LOAD);
		chooser.setTitle(file ? "Open File" : "Open Folder");
		if (null != folder) {
			chooser.setDirectory(folder.getAbsolutePath());
		}

		chooser.setVisible(true);

		String fileName = chooser.getFile();
		String dirName = chooser.getDirectory();
		log.info("Result dir=" + dirName + ", file=" + fileName);
		if (null == dirName || null == fileName) {
			return null;
		}
		return file ? new File(dirName, fileName) : new File(dirName);
	}

	public static File getFile(String path) {
		if (null == path || 0 == path.trim().length()) {
			return null;
		} else {
			return new File(path);
		}
	}
}
